<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2019/4/26
 * Time: 17:46
 */
namespace Modules\Admin\Main\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Activity;
use Illuminate\Http\Request;

class WelfareController extends Controller{
    public function search(){
        return view("main::welfare",\Request::all());
    }

    public function apiSearch(Request $request){
        $limit = $request->input('limit', 10); // 这个是接受参数
        $offset = $request->input('offset', 0);
        $data = [];

        $data['total'] = Activity::query()->count();
        $data['items'] = Activity::query()
            ->take($limit)
            ->skip($offset)
            ->orderBy('id', 'desc')
            ->get();

        return success($data);
    }

    public function apiAdd(Request $request){
        $input = $request->only(["title","image_url","redirect_to","h5_url","status"]);
        $query = Activity::query()->insertGetId($input);
        if ($query)return success();
    }

    public function apiUpdate(Request $request){
        $id = $request->input("id");
        $input = $request->only(['title','image_url','redirect_to','h5_url','status']);
        if ($id){
            $query = Activity::query()->where("id",$id)->update($input);
        }
        return success();
    }

    public function apiDelete(Request $request){
        $id = $request->input("id");
        if ($id){
            $query = Activity::query()->where("id",$id)->delete();
        }
        return success();
    }
}